package com.emip.servlet.baseinfo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emip.bean.ValleyBean;
import com.emip.config.SysParams;
import com.emip.pojo.Table;
import com.emip.service.ValleyService;
import com.emip.util.TableFormat;


@Controller
@RequestMapping(value="/base/valley")
public class ValleyAction {

	@Autowired
	ValleyService valleyService;
	
	@RequestMapping(value="/management")
	public String index() {		
		return "/baseInfo/ValleyManagement";
	}
	
	@RequestMapping(value="/getAllValleys")
	public @ResponseBody Table<ValleyBean> getAllValleys() {
		List<ValleyBean> result = valleyService.getAll();
		int records = result.size();
		int totalPages = records / SysParams.gridDefinedRowNumber + 1;
		return new TableFormat<ValleyBean>().formTable(totalPages, 1, records, result);

	}
	
	@RequestMapping(value="/getValleyByID",method=RequestMethod.POST, headers="Accept=application/json")
	public @ResponseBody ValleyBean getValleyByID(@RequestParam("eid") int eid)
	{
		return valleyService.getValleyByID(eid);
	}
	
	@RequestMapping(value="/addValley",method=RequestMethod.POST)
	public @ResponseBody ValleyBean addValley(@RequestParam("eName") String name, @RequestParam("eWaterSystem") String waterSystem,
			@RequestParam("eAlongSide") String alongSide) {
		return valleyService.addValley(name, waterSystem, alongSide);
	}
	
	@RequestMapping(value="/updateValley",method=RequestMethod.POST)
	public @ResponseBody ValleyBean updateValley(@RequestParam("eID") int id, @RequestParam("eName") String name, @RequestParam("eWaterSystem") String waterSystem,
			@RequestParam("eAlongSide") String alongSide) {
		return valleyService.updateValley(id, name, waterSystem, alongSide);
	}
	
	@RequestMapping(value="/deleteValley",method=RequestMethod.POST)
	public @ResponseBody String deleteValley(@RequestParam("eID") int id) {
		valleyService.deleteValley(id);
		return "success";
	}
	
	@RequestMapping(value="/getAllValleysObj")
	public @ResponseBody List<ValleyBean> getAllValeysObj() {
		return valleyService.getAll();
	}
}
